const express = require('express');
const router = express.Router();
const authMiddleware = require('../middleware/authMiddleware');
const adminMiddleware = require('../middleware/adminMiddleware');
const sellerController = require('../controllers/sellerController');
const buyerController = require('../controllers/buyerController');
const adminController = require('../controllers/admincontroller');

/**
 * 交易API路由集成
 * 包含卖家、买家和管理员的交易相关API
 */

// 公共中间件 - 所有路由需要认证
router.use(authMiddleware);

/**
 * 卖家交易API
 */
router.get('/seller/orders', sellerController.getOrders);
router.put('/seller/orders/:order_id/status', sellerController.updateOrderStatus);
router.put('/seller/orders/:order_id/cancel', sellerController.cancelOrder);

/**
 * 买家交易API
 */
router.post('/orders', buyerController.createOrder);
router.post('/transactions', buyerController.createTransaction);
router.get('/buyer/orders', buyerController.getOrders);
router.put('/buyer/orders/:order_id/pay', buyerController.payOrder);
router.put('/buyer/orders/:order_id/status', buyerController.updateOrderStatus);
router.put('/buyer/orders/:order_id/cancel', buyerController.cancelOrder);

/**
 * 管理员交易API
 * 需要管理员权限
 */
router.get('/admin/orders', adminMiddleware, adminController.getTransactions);
router.get('/admin/orders/:order_id', adminMiddleware, adminController.getTransactionDetail);
router.put('/admin/orders/:order_id/status', adminMiddleware, adminController.updateTransactionStatus);
router.put('/admin/orders/:order_id/cancel', adminMiddleware, adminController.cancelTransaction);

module.exports = router;